<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/cafe/_init_model_cafe.php';	
	
	class mod_cafe_img_gallery extends _init_model_cafe{
		public $CAF_IMG_FILENAME;
		public $CAF_IMG_DESC;
		public $CAF_IMG_DATE;
		public $CAF_IMG_TIME;
		public $CAF_ID;
		
		function mod_cafe_img_gallery(){
			parent::__construct();
					
			$this->tableName = "cafe_img";
			
			$this->load->library('custom_datetime');
			$this->CAF_IMG_DATE = $this->custom_datetime->current_date();
			$this->CAF_IMG_TIME = $this->custom_datetime->current_time();
		}
		
		function get_gallery_list($cafe_id=null, $desc=true, $fields=null){
			if (!is_null($fields))
				$this->db->select($fields);
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);			
			if ($desc)
				$this->db->order_by('CAF_IMG_DATE, CAF_IMG_TIME', 'DESC');
			
			$query = $this->db->get($this->tableName);
			$return = $query->result_array();
			
			if (count($return) > 0){
				
				$this->load->library('define/directory_path');			
				for ($i=0; $i<count($return); $i++){
					if (isset($return[$i]['CAF_IMG_FILENAME']))										
						$return[$i]['CAF_IMG_FILENAME'] = $this->directory_path->img_cafe_gallery_path . $return[$i]['CAF_IMG_FILENAME'];			
				}
			}
			
			return $return;
		}
		function get_current_img_count($cafe_id=null){
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);
			return $this->db->count_all_results($this->tableName);
		}
		
		function add(){
			$this->load->library('custom_datetime');
			$this->load->model('local/data/cafe/mod_cafe');
			
			
			$data = array('CAF_IMG_FILENAME' => $this->CAF_IMG_FILENAME,
							'CAF_IMG_DESC' => $this->CAF_IMG_DESC,
							'CAF_IMG_DATE' => $this->CAF_IMG_DATE,
							'CAF_IMG_TIME' => $this->CAF_IMG_TIME,
							'CAF_ID' => $this->CAF_ID);
			return $this->db->insert($this->tableName, $data);
		}
		
		function remove($filename, $cafe_id=null){
			
			// remove file from server
			$this->load->library('define/directory_path');
			unlink($this->directory_path->img_upload_cafe_gallery_path . $filename);
			
			$this->db->where('CAF_IMG_FILENAME', $filename);
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);
						
			return $this->db->delete($this->tableName);
		}
		function remove_multiple($filename_list, $cafe_id=null){
			$this->load->library('define/directory_path');
			foreach ($filename_list as $item){
				unlink($this->directory_path->img_upload_cafe_gallery_path . $item);
			}
			
			$this->db->where_in('CAF_IMG_FILENAME', $filename_list);
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);
			
			return $this->db->delete($this->tableName);
		}
		function remove_by_cafe($cafe_id){			
			$result = $this->get_gallery_list($cafe_id, false, array('CAF_IMG_FILENAME'));

			$this->load->library('define/directory_path');
			for ($i=0; $i<count($result); $i++){
				unlink($this->directory_path->img_upload_cafe_gallery_path . $result[$i]['CAF_IMG_FILENAME']);
			}

			return parent::remove_by_cafe($cafe_id);
		}
	}
?>